import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from "@/components/admin/Main";
import AdminInfo from "@/components/root_admin/AdminInfo";
import NewsAdd from "@/components/admin/NewsAdd";
import UserInfo from "@/components/admin/UserInfo";
import ArticleAdd from "@/components/admin/ArticleAdd";
import NewsInfo from "@/components/admin/NewsInfo";
import ArticleInfo from "@/components/admin/ArticleInfo";
import Login from "@/views/Login";
import Index from "@/views/Index"
import Register from "@/views/Register";
import custom_index from "@/components/custom/custom_index";
import custom_login from "@/components/custom/custom_login";
import chat from "@/components/custom/chat";
import AssessInfo from "@/components/admin/AssessInfo";
import QuestionShow from "@/components/admin/QuestionShow";
import root_admin from "@/views/root_admin";
import Custom from "@/views/Custom";
import custom_assess from "@/components/custom/custom_assess";
import custom_home from "@/components/custom/custom_home";
import root_index from "@/components/root_admin/root_index";
import admin_home from "@/components/admin/admin_home";
import root_adminadd from "@/components/root_admin/root_adminadd";
import UserAccount from "@/components/admin/UserAccount";
import assess_info from "@/components/custom/assess_info";

const originalPush = VueRouter.prototype.push;

Vue.use(VueRouter)

VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => {
        err
    })
}

const router = new VueRouter({
    routes: [
        {
            path: '/Register',
            name: 'Register',
            component: Register

        },
        /*        {
                    path: '/custom_index',
                    name: 'custom_index',
                    meta: {
                        requireAuth: true  // 添加该字段，表示进入这个路由是需要登录的
                    },
                    component: custom_index
                },*/
        {
            path: '/',
            name: 'Custom',
            component: Custom,
            redirect: '/Custom/custom_index'
        },

        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/Index',
            component: Index,
            children: [
                {
                    path: 'Main',
                    component: Main
                },
                {
                    path: 'UserInfo',
                    component: UserInfo
                },
                {
                    path: 'NewsAdd',
                    component: NewsAdd
                },
                {
                    path: 'UserInfo',
                    component: UserInfo
                },
                {
                    path: 'ArticleAdd',
                    component: ArticleAdd
                },
                {
                    path: 'NewsInfo',
                    component: NewsInfo
                },
                {
                    path: 'ArticleInfo',
                    component: ArticleInfo
                },
                {
                    path: 'AssessInfo',
                    component: AssessInfo
                },
                {
                    path: 'QuestionShow',
                    component: QuestionShow
                },
                {
                    path: 'admin_home',
                    component: admin_home
                },
                {
                    path: 'UserAccount',
                    component: UserAccount
                }
            ]
        },
        {
            path: '/Custom',
            component: Custom,
            children: [
                {
                    path: 'custom_index',
                    component: custom_index,
                },
                {
                    path: 'custom_login',
                    component: custom_login,
                },
                {
                    path: 'chat',
                    component: chat
                },
                {
                    path: 'custom_assess',
                    component: custom_assess,
                },
                {
                    path: 'assess_info',
                    component: assess_info
                },
                {
                    path: 'custom_home',
                    component: custom_home
                }
            ]
        },
        {
            path: '/root_index',
            component: root_index,
            children:[
                {
                    path: 'AdminInfo',
                    component: AdminInfo
                },
                {
                    path: 'root_adminadd',
                    component: root_adminadd
                }
            ]
        },
        {
            path: '/root_admin',
            name: 'root_admin',
            component: root_admin
        }
    ]
})


router.beforeEach((to, from, next) => {
    //首先判断用户是否已经登录
    const token = JSON.parse(window.sessionStorage.getItem("token"));
    if (to.path === "/login" || to.path ==="/Custom" || to.path ==="/root_admin"
        || to.path ==="/Custom/custom_index"
        || to.path ==="/Custom/chat"
        || to.path ==="/Custom/custom_assess"
        || to.path ==="/Custom/custom_home"
        || to.path ==="/Custom/custom_login"
    ) {
        next();
    } else {
        if (token) {
            //已经登录
            next();
        } else {
            next("/login")
        }
    }
})


export default router
